package com.chuan.icons;

import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class Parameter extends Activity {
	private Menu mMenu;

	private LinearLayout.LayoutParams LP_FF = new LinearLayout.LayoutParams(
			LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
	private LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(
			LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	private LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(
			LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

	private HashMap<String, EditText> mParamObject = new HashMap<String, EditText>();
	private Button mDoneButton = null;
	private String mSiteName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent it = getIntent();
		mSiteName = it.getStringExtra("site");
		if (mSiteName.length() < 1) {
			finish();
		}

		SiteBase sb = SiteBase.getSiteClass(mSiteName, null);
		if (sb == null) {
			return;
		}
		JSONArray ary = sb.getAccountInfo();

		setTitle(mSiteName + " account info :");

		ScrollView sv = new ScrollView(this);
		sv.setLayoutParams(LP_FF);

		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setPadding(5, 5, 5, 5);

		mParamObject.clear();
		for (int i = 0; i < ary.length(); ++i) {
			try {
				addNewParam(this, layout, ary.getString(i));
			} catch (JSONException e) {
				continue;
			}
		}

		addDoneButton(this, layout);

		sv.addView(layout);
		setContentView(sv);
	}

	private void addNewParam(Context context, LinearLayout layout, String title) {
		LinearLayout param_layout = new LinearLayout(context);
		param_layout.setOrientation(LinearLayout.HORIZONTAL);
		param_layout.setLayoutParams(LP_FW);
		param_layout.setPadding(5, 5, 5, 5);

		TextView tv = new TextView(context);
		tv.setLayoutParams(LP_WW);
		tv.setText(title);
		tv.setPadding(0, 0, 20, 0);
		tv.setTextSize(17);

		EditText et = new EditText(context);
		et.setLayoutParams(LP_FW);

		if (title.toLowerCase().equals("password")) {
			et.setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
			et.setTransformationMethod(new PasswordTransformationMethod());
		}

		param_layout.addView(tv);
		param_layout.addView(et);

		mParamObject.put(title, et);
		layout.addView(param_layout);
		return;
	}

	private void addDoneButton(Context context, LinearLayout layout) {
		LinearLayout param_layout = new LinearLayout(context);
		param_layout.setOrientation(LinearLayout.HORIZONTAL);
		param_layout.setLayoutParams(LP_FW);
		param_layout.setPadding(5, 5, 5, 5);

		mDoneButton = new Button(this);
		mDoneButton.setText(getResources().getString(R.string.str_add_account));

		mDoneButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				newAccountParameter();
			}
		});

		param_layout.addView(mDoneButton);
		layout.addView(param_layout);
		return;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		mMenu = menu;
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.account_parameter, mMenu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.AddAccountParameter:
			newAccountParameter();
			return true;

		default:
			break;
		}

		return false;
	}

	private void newAccountParameter() {
		String title = null;
		String param = null;
		EditText et = null;
		Iterator<String> iterator = mParamObject.keySet().iterator();

		JSONObject new_account = new JSONObject();
		try {
			new_account.put(Constants.PARAMETER_SITE, mSiteName);
		} catch (JSONException e1) {
			return;
		}
		while (iterator.hasNext()) {
			title = (String) iterator.next();
			et = mParamObject.get(title);
			param = et.getText().toString();
			if (param.length() < 1) {
				showInfo("Field empty:" + title);
				return;
			}

			try {
				new_account.put(title, param);
			} catch (JSONException e) {
				return;
			}
		}

		try {
			LiteDB.addAccounts(new_account.getString("username"), new_account
					.getString("site"), new_account);
		} catch (JSONException e1) {
			showInfo("Add account fail.");
			return;
		}

		showInfo("Add account success.");
		closeParamActivity(true);
	}

	private void showInfo(String info) {
		Toast.makeText(this, info, Toast.LENGTH_SHORT).show();
	}

	private void closeParamActivity(boolean success) {
		Intent back = new Intent();
		back.putExtra("addSuccess", success);
		setResult(Account.RET_ACCOUNT_SUCCESS, back);

		finish();
	}
}
